package com.mgr.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mgr.dao.CustomerDao;
import com.mgr.model.Customers;
import com.mgr.service.OTPService;

@Service
public class OTPServiceImpl implements OTPService {
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Transactional
	public boolean sendEmail(int custID, String codeOTP) {
		Customers customer = customerDao.getCustomers(custID);
		boolean isSend = false;
		if(customer != null ){
			String emailAdd = customer.getEmail();
			// creates a simple e-mail object
			SimpleMailMessage email = new SimpleMailMessage();
			email.setTo(emailAdd);
			email.setSubject("OTP code");
			email.setText(codeOTP);
			
			// sends the e-mail
			mailSender.send(email);
			isSend = true;
		}
		return isSend;
	}

}
